import pandas as pd
import os
import glob
import re
from program.pack.append_df_to_excel import append_df_to_excel

import numpy as np
import warnings
# 导入shutil模块和os模块
import shutil, os

# 忽略警告
warnings.filterwarnings("ignore")

# 列显示不全，进行设置
pd.set_option('display.max_columns', 500)
pd.set_option('display.unicode.ambiguous_as_wide', True)
pd.set_option('display.unicode.east_asian_width', True)
pd.set_option('display.width', 180)  # 设置打印宽度(**重要**)

# 查询指定execl [data_xlsx]中，所有的第3张图片（正面、背面）图，并按照规则进行重命名图片，存放至path_out文件夹中

# 样品名称
l_1 = ['陈基杰', '黄静君', '黎金兰', '刘松朗', '刘子韬', '苏菲', '杨小英']
l_2 = ['front_4', 'back_4']
for i in l_1:
    for j in l_2:
        print(i, j)
        path = r'..\\data\\' + i + j[0]
        isExists = os.path.exists(path)
        if not isExists:
            os.makedirs(path)
            print(path + '创建成功')
        else:
            print(path + '目录已存在')
        data_xlsx = pd.read_excel(r'C:\Users\wangshuan\Desktop\新建文件夹 (6)\2021-4-14-30-test.xlsx', sheet_name='data')
        data_xlsx = data_xlsx.drop(data_xlsx[data_xlsx['姓名'] != i].index)
        # data_xlsx = data_xlsx.drop(data_xlsx[data_xlsx['姓名'] != i].index)
        # data_xlsx = data_xlsx.drop(data_xlsx[data_xlsx['文件名'] == '2021-07-19-28#-jichang-1.xlsx'].index)

        data_01 = data_xlsx
        data_01['地址'] = data_01['originImage地址'] + '\\' + data_01['originImage文件名'] + '\\' + j + '.bmp'

        data_01['fb序号'] = range(1, len(data_01) + 1)
        data_01['fb序号'] = data_01['fb序号'].apply(lambda x: str(x).rjust(3, '0'))
        data_01['fb序号'] = data_01['fb序号'] + '_' + j[0] + '_' + data_01['性别'] + '_' + data_01['BMI'].astype(str)
        # 6位数，补0
        # data_01['fb序号'] = data_01['fb序号'].apply(lambda x: str(x).rjust(6, '0'))

        append_df_to_excel(path + '\\' + i + j[0] + '.xlsx', data_01, sheet_name='data', startcol=0, startrow=0,
                           index=False)
        # 复制文件
        data_01 = data_01.reset_index()

        path_out = r'..\\data\\' + i + j[0] + '\\\\'
        for k in range(len(data_01)):
            file_path1 = data_01.loc[k, '地址']
            file_path2 = path_out + data_01.loc[k, 'fb序号'] + '.bmp'
            shutil.copy(file_path1, file_path2)
